package com.zzz.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/*
 * @author zzr
 * @date: 2025/07/03  20:49
 * @description: 验证用户名的Servlet
 */
public class userServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1. 获取请求中的键值对参数 key=value （根据参数名获取参数 无论参数在 usl? 后面，还是在请求体中，都能获取）
        String username = request.getParameter("username");

        // 2.处理业务的代码
        String info = "<h1>YES</h1>";
        if ("admin".equals(username)) {
            info = "<h1>NO</h1>";
        }

        // 3. 将要响应的数据放入 response
        // 应该先设置 Content-Type 响应头
        // response.setHeader("Content-Type","text/html");
        response.setContentType("text/html");
        PrintWriter writer = response.getWriter(); //该方法返回的是一个向响应体中打印字符串的打印流
        writer.write(info);
    }
}
